package com.juc.learn.sync;

/**
 * ClassName:TestShareNumber
 * Package:com.juc.learn.sync
 * Description:
 *
 * @Author:@郭先森
 * @Create:2025/7/26 - 16:25
 * @Version:v1.0
 */
public class TestShareNumber {
    public static void main(String[] args) {
        ShareNumber number = new ShareNumber();
        new Thread(()->{
            while (true){
                try {
                    number.add();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        new Thread(()->{
            while (true){
                try {
                    number.sub();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        //注意：如果在下边再创建两个线程，一个做add一个做sub，可能会有虚假唤醒的问题

        new Thread(()->{
            while (true){
                try {
                    number.add();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        new Thread(()->{
            while (true){
                try {
                    number.sub();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }
}
